<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Auth extends MY_Controller {
	public $_data = array();

	public function __construct() {
		parent::__construct();
		$this->load->model('auth_model', 'auth');
	}

	public function index() {
		$nodes = $this->auth->get_nodes();

		$rules = node_merge($nodes);

		$this->_data['nodes'] = $rules;
		// dump($rules);
		$this->load->view('admin/node_list.html', $this->_data);
	}

	public function set_access() {
		$is_handle = $this->input->post('post');
		$gid = $this->uri->segment(4);
		if ($is_handle) {
			$checked_inputs = $this->input->post('rules');
			$rules = implode(',', $checked_inputs);
			$data = array(
				'rules' => $rules
				);
			$status = $this->auth->set_access($gid, $data);
			if ($status) {
				redirect('admin/auth/role');
			}

		} else {
			$nodes = $this->auth->get_nodes();
			$access = '';
			//读取用户原有权限
			$g_access = $this->auth->get_access($gid);
			if ($g_access) {
				$access = explode(',', $g_access['rules']);
			}
			$rules = node_merge($nodes, $access);
			$this->_data['nodes'] = $rules;

			$this->load->view('admin/access.html', $this->_data);
		}
		
	}

	/**
	 * 角色管理
	 * @return [type] [description]
	 */
	public function role() {
		$gid = $this->uri->segment(4);
		if (empty($gid)) {
			$this->_add_role();
		} else {
			$this->_edit_role($gid);
		}
	}

	/**
	 * 添加角色
	 */
	private function _add_role() {
		$this->load->library('form_validation');
		$status =$this->form_validation->run('group');
		if (false == $status) {
			$this->_data['roles'] = $this->auth->get_roles();
			$this->load->view('admin/role.html', $this->_data);
		} else {
			$data = array(
				'title' =>$this->input->post('title'),
				'status' =>$this->input->post('status'),
				);
			$gid = $this->auth->add_role($data);
			if (!$gid) {
				error('添加角色失败');
			} 
			redirect('admin/auth/role');
		}
	}

	/**
	 * 编辑角色
	 * @param  [type] $gid [description]
	 * @return [type]      [description]
	 */
	private function _edit_role($gid) {
		$this->load->library('form_validation');
		$status = $this->form_validation->run('group');
		// $this->output->enable_profiler(TRUE);
		if (false == $status) {
			$this->_data['role'] = $this->auth->get_role($gid);
			$this->_data['roles'] = $this->auth->get_roles();
			$this->load->view('admin/role.html', $this->_data);
		} else {
			$data = array(
				'title' => $this->input->post('title'),
				'status' => $this->input->post('status'),
				);
			$this->auth->edit_role($gid, $data);
			redirect('admin/auth/role');
		}
	}

	/**
	 * 添加节点
	 */
	public function add_node() {
		$pid = $this->uri->segment(4);
		$this->_add_node($pid);
	}

	/**
	 * 动作节点管理
	 */
	public function node() {
		$rid = $this->uri->segment(4);
		if (empty($rid)) {
			$this->_add_node();
		} else {
			$this->_edit_node($rid);
		}
	}

	private function _add_node($pid) {
		$this->load->library('form_validation');
		$status = $this->form_validation->run('auth');

		if (false == $status) {
			$this->_data['pid'] = $pid ? $pid : 0;
			$this->load->view('admin/node.html', $this->_data);
		} else {
			$data = array(
				'pid' => $this->input->post('pid'),
				'name' => $this->input->post('name'),
				'title' => $this->input->post('title'),
				'status' => $this->input->post('status'),
				'orderby' => $this->input->post('orderby')
				);
			$this->load->model('auth_model', 'auth');
			$rid = $this->auth->add_node($data);
			if (!$rid) {
				error('添加节点失败');
			}

			redirect('admin/auth/');

		}
	}

	public function _edit_node($rid) {
		$this->load->library('form_validation');
		$status = $this->form_validation->run('auth');
		if (false == $status) {
			$node = $this->auth->get_node($rid);
			$this->_data['pid'] = $node['pid'];
			$this->_data['node'] = $node;
			$this->load->view('admin/node.html', $this->_data);
		} else {
			$data = array(
				'pid' => $this->input->post('pid'),
				'name' => $this->input->post('name'),
				'title' => $this->input->post('title'),
				'status' => $this->input->post('status'),
				'orderby' => $this->input->post('orderby')
				);
			$status = $this->auth->edit_node($rid, $data);
			if ($status) {
				error('更新失败');
			}
			redirect('admin/auth');

		}
	}

}

/* End of file auth.php */
/* Location: ./application/controllers/admin/auth.php */